
<?php
require_once  __DIR__ . '/includes/authenticate.php';

if (isset($_POST['user_key'])) {
	
	$user_key = $_POST['user_key'];

	$season = $_POST['season'];

	include_once './db_bb_connect.php';

	$sql = "SELECT firstname, lastname, username, playernumber, position, email, phonenr, birthdate, height, allowed
	FROM users
	WHERE user_key = '$user_key'";

	$result = $db->query($sql);
	$row = $result->fetch_assoc();

	$firstname = $row['firstname'];
	$lastname = $row['lastname'];
	$username = $row['username'];
	$playernumberold = $row['playernumber'];
	$position = $row['position'];
	$email = $row['email'];
	$phonenr = $row['phonenr'];
	$birthdate = $row['birthdate'];
	$height = $row['height'];
	$allowed = $row['allowed'];

	if ($allowed == 1) {
		$allowedstate = 'Yes';
	} else {
		$allowedstate = 'No';
	}
	
	$sql = "SELECT playernumber
	FROM teams
	WHERE playerID = '$user_key'
	AND season = '$season'";

	$result = $db->query($sql);
	$row = $result->fetch_assoc();

	$playernumber = $row['playernumber'];

?>

<style>
  input[type='number'] {
    -moz-appearance:textfield;
}
</style>

<div class="container-fluid">
<div class="row">
<br/>
<h3>Edit Profile of <?= $firstname; ?> <?= $lastname; ?></h3>
<br/>
<form class="form-horizontal"> 
	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Firstname:</label>
		<div class="col-sm-8">
			<input class="form-control" type="text" name="firstname" value="<?= $firstname; ?>"/>
		</div>
	</div>
	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Lastname:</label>
		<div class="col-sm-8">
			<input class="form-control" type="text" name="lastname" value="<?= $lastname; ?>"/>
		</div>
	</div>
	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Username:</label>
		<div class="col-sm-8">
			<input class="form-control" type="text" name="username" value="<?= $username; ?>"/>
		</div>
	</div>	

	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Birthdate (yyyy-mm-dd):</label>
		<div class="col-sm-8">
			<input class="form-control" type="date" name="birthdate" value="<?= $birthdate; ?>" required/>
		</div>
	</div>

	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Position:</label>
		<div class="col-sm-8">
		<select class="form-control" name="position">
			<option selected="selected" value="<?= $position; ?>"><?= ucfirst($position); ?></option>
			<option value="center">Center</option>
			<option value="forward">Forward</option>
			<option value="guard">Guard</option>
		</select>
			
		</div>
	</div>

	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Player Number:</label>
		<div class="col-sm-8">
			<input class="form-control" type="number" name="playernumber" value="<?= $playernumber; ?>" required/>
		</div>
	</div>
	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Phone Number:</label>
		<div class="col-sm-8">
			<input class="form-control" type="text" name="phonenr" value="<?= $phonenr; ?>"/>
		</div>
	</div>

	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Email:</label>
		<div class="col-sm-8">
			<input class="form-control" type="text" name="email" value="<?= $email; ?>"/>
		</div>
	</div>

	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Height (cm):</label>
		<div class="col-sm-8">
			<input class="form-control" type="text" name="height" value="<?= $height; ?>" required/>
		</div>
	</div>

	<div class="form-group">
		<label class="control-label col-sm-4" for="usr">Login Allowed:</label>
		<div class="col-sm-8">
			<select class="form-control" name="allowed">
			<option selected="selected" value="<?= $allowed; ?>"> <?= $allowedstate; ?></option>
			<option value="1">Yes</option>
			<option value="2">No</option>
			</select>
		</div>
	</div>
	<input type="hidden" name="user_key" value="<?= $user_key; ?>">
	<div class="form-group">
		<div class="col-sm-offset-3 col-sm-6 col-md-offset-3 col-md-6">
		<button class="btn btn-success" type="submit" name="submit">Update </button>
		</div>
	</div>

<input type='hidden' name='season' value='<?= $season; ?>'/>

</form>

</div>
</div>
<script type="text/javascript">

$("form").on("submit", function(e) {
    postData = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "./includes/handlers/playerprofile_editgeneral_handler.php",
        data: postData,
        success: function(data) { 
             $("#playerprofilecontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#playerprofilecontent").html(data);
        }
      }); 
    e.preventDefault();

});  

</script>


<?php

}

?>


